package ano.it.controller;

import ano.it.constant.MessageConstant;
import ano.it.entity.Result;
import ano.it.pojo.DetailedData;
import ano.it.service.ReportService;
import ano.it.utils.DownFile;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private ReportService reportService;
        @RequestMapping("/getBusinessReportData")
        public Result getBusinessReportData(){
                List<DetailedData> list=new ArrayList<DetailedData>();
                try {
                        list = reportService.getBusinessReportData();
                        return new Result(true,MessageConstant.GET_REPOT_SUCCESS,list);
                } catch (Exception e) {
                        e.printStackTrace();
                        return new Result(false,MessageConstant.GET_REPOT_FAIL);
                }
        }
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws IOException {


            String filePath = request.getSession().getServletContext().getRealPath("template")+ File.separator+"dapengxinxi.xlsx";
            //基于提供的Excel模板文件在内存中创建一个Excel表格对象
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            List<Map<String, String>> xiazai = DownFile.xiazai();
            int i = 2;
            for ( Map<String, String>  result  :xiazai) {
                    //取出返回结果数据，准备将报表数据写入到Excel文件中
                    String id = (String) result.get("id");
                    String crop= (String) result.get("crop");
                    String crop_num = (String) result.get("crop_num");
                    String grow = (String) result.get("grow");
                    String room_wendu = (String) result.get("room_wendu");
                    String kong_shidu = (String) result.get("kong_shidu");
                    String tu_shi = (String) result.get("tu_shi");
                    String ph = (String) result.get("ph");
                    String co2 = (String) result.get("co2");
                    String sun = (String) result.get("sun");
                    String kong_zhi = (String) result.get("kong_zhi");
                    String buguang = (String) result.get("buguang");
                    String shexiang = (String) result.get("shexiang");
                    String jiaoguan = (String) result.get("jiaoguan");
                    String chuangru = (String) result.get("chuangru");
                    String huanjing = (String) result.get("huanjing");
                    String fuzeren = (String) result.get("fuzeren");


                    //读取第一个工作表
                    XSSFSheet sheet = excel.getSheetAt(0);

                            XSSFRow row = sheet.createRow(i);
                            row.createCell(0).setCellValue(id);
                            row.createCell(1).setCellValue(crop);
                            row.createCell(2).setCellValue(crop_num);
                            row.createCell(3).setCellValue(grow);
                            row.createCell(4).setCellValue(room_wendu);
                            row.createCell(5).setCellValue(kong_shidu);
                            row.createCell(6).setCellValue(tu_shi);
                            row.createCell(7).setCellValue(ph);
                            row.createCell(8).setCellValue(co2);
                            row.createCell(9).setCellValue(sun);
                            row.createCell(10).setCellValue(kong_zhi);
                            row.createCell(11).setCellValue(buguang);
                            row.createCell(12).setCellValue(shexiang);
                            row.createCell(13).setCellValue(jiaoguan);
                            row.createCell(14).setCellValue(chuangru);
                            row.createCell(15).setCellValue(huanjing);
                            row.createCell(16).setCellValue(fuzeren);
                    i++;
            }
           /* Map<String,Object> result = new HashMap<String,Object>();
            //取出返回结果数据，准备将报表数据写入到Excel文件中
            Integer id = (Integer) result.get("id");
            String crop= (String) result.get("crop");
            Integer crop_num = (Integer) result.get("crop_num");
            String grow = (String) result.get("grow");
            String room_wendu = (String) result.get("room_wendu");
            String kong_shidu = (String) result.get("kong_shidu");
            String tu_shi = (String) result.get("tu_shi");
            String ph = (String) result.get("ph");
            String co2 = (String) result.get("co2");
            String sun = (String) result.get("sun");
            String kong_zhi = (String) result.get("kong_zhi");
            String buguang = (String) result.get("buguang");
            String shexiang = (String) result.get("shexiang");
            String jiaoguan = (String) result.get("jiaoguan");
            String chuangru = (String) result.get("chuangru");
            String huanjing = (String) result.get("huanjing");
            String fuzeren = (String) result.get("fuzeren");

            String filePath = request.getSession().getServletContext().getRealPath("template")+ File.separator+"dapengxinxi.xlsx";
            //基于提供的Excel模板文件在内存中创建一个Excel表格对象
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //读取第一个工作表
            XSSFSheet sheet = excel.getSheetAt(0);

            for (int  i = 2; i <= 12;i++){
            XSSFRow row = sheet.createRow(i);
            row.createCell(0).setCellValue(id);
            row.createCell(1).setCellValue(crop);
            row.createCell(2).setCellValue(crop_num);
            row.createCell(3).setCellValue(grow);
            row.createCell(4).setCellValue(room_wendu);
            row.createCell(5).setCellValue(kong_shidu);
            row.createCell(6).setCellValue(tu_shi);
            row.createCell(7).setCellValue(ph);
            row.createCell(8).setCellValue(co2);
            row.createCell(9).setCellValue(sun);
            row.createCell(10).setCellValue(kong_zhi);
            row.createCell(11).setCellValue(buguang);
            row.createCell(12).setCellValue(shexiang);
            row.createCell(13).setCellValue(jiaoguan);
            row.createCell(14).setCellValue(chuangru);
            row.createCell(15).setCellValue(huanjing);
            row.createCell(16).setCellValue(fuzeren);
            }*/
            //使用输出流进行表格下载,基于浏览器作为客户端下载
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");//指定以附件形式进行下载
            excel.write(out);

            out.flush();
            out.close();
            excel.close();
            return null;

    }
}
